<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <img id="tu" src="../lagou-js/img/1.jpg" width="50%">
    <br/>
    <button onclick="begin()">开始</button>
    <button onclick="stop()">停止</button>

    <script>
       /* 案例：模拟年会抽奖 */
       var arr = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg"];
       function begin(){
           timer = setInterval(bian,100);//没有使用var,所以timer是全局变量
       }

       function stop(){
           clearInterval(timer); //停止定时器
       }

       function bian(){
           var i = Math.floor(Math.random()*arr.length); //0-4
           document.getElementById("tu").src = "../lagou-js/img/" + arr[i];
       }


    </script>
</body>
</html>